package com.jwell.panrank.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panrank.R
import com.jwell.panrank.application.App
import com.jwell.panrank.contract.WaybillContract
import com.jwell.panrank.entity.WaybillBean
import com.jwell.panrank.model.WaybillModel
import com.jwell.panrank.presenter.WaybillPresenter
import com.jwell.panrank.ui.adapter.WaybillAdapter
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 运单管理
 */
class WaybillActivity : MVPBaseActivity<WaybillPresenter>(), WaybillContract.View {

    private var searchData = WaybillModel.RequestData()
    private var listData = ArrayList<WaybillBean>()
    private var adapter = WaybillAdapter(listData)
    override fun createPresenter(): WaybillPresenter? {
        return WaybillPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_waybill)
        initToolBar("运单管理", true, null)
        toolbar_right_image_serarh.visibility = View.VISIBLE
        initData()
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.VISIBLE
//        searchData.type = 3
        presenter?.searchListData(searchData)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
        //搜索电子磅单
        toolbar_right_image_serarh.setOnClickListener {
            var intent = Intent(this, WaybillSearchActivity::class.java)
            startActivity(intent)
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "暂无运单数据"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun showListData(data: List<WaybillBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < 10) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun setSearchTotalCount(count: Int) {

    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}
